# author:  freelaeder
# ----
# date:  2022/4/12 9:48
import requests


class Tieba:
    def __init__(self):
        # 地址
        self.url = 'https://tieba.baidu.com/f?kw=python'
        # 定义请求头
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36',
            'Cookie': 'BIDUPSID=92EFA278A1789E4E859A0E54B2E5DBE4; PSTM=1632566244; __yjs_duid=1_ad08f1db388f2e635d3440ad35fe1bed1632575354481; ISSW=1; BD_UPN=12314753; H_WISE_SIDS=110085_127969_177370_178384_178629_179350_179452_181133_181485_181588_182231_182531_183035_183328_183981_184011_184321_184440_184560_184655_184737_184794_184826_184893_185224_185268_185517_185879_186318_186597_186635_186716_186831_186844_186897_187022_187067_187090_187184_187188_187205_187292_187356_187433_187449_187669_187726_187912_187929_187960_188031_188181_188296_188332_188352_188427_188463_188615_188720_188731_188846_188900_189058_189325_189390_189418_189431_189468_189679_189716_189755_190113_190153_190157; ISSW=1; ORIGIN=2; MSA_WH=411_731; BDUSS=xpbUNNalY5d05XaEhzcmN6aFN4RHItZVdqdUtmOXRJU3dTYk5aU2gwNjAwTjloSVFBQUFBJCQAAAAAAAAAAAEAAADSfjqEz6e7qNa7zqrs8gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALRDuGG0Q7hhZU; BDUSS_BFESS=xpbUNNalY5d05XaEhzcmN6aFN4RHItZVdqdUtmOXRJU3dTYk5aU2gwNjAwTjloSVFBQUFBJCQAAAAAAAAAAAEAAADSfjqEz6e7qNa7zqrs8gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALRDuGG0Q7hhZU; BAIDUID=6B534E672C05F9F85A5E1061E3C79346:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=31254_35910_36167_34584_36140_35978_36126_36234_26350_35867_36100_36061; ZD_ENTRY=baidu; delPer=0; BD_CK_SAM=1; PSINO=3; BAIDUID_BFESS=0C21235976974FA7DE655BE192000F01:FG=1; BD_HOME=1; ab_sr=1.0.1_YTMwZjYyOTFmYmQxNjc4OGE4MTYxYmYyOTUxMTJkZjE0N2RjMzBhMTY4Y2FkZWEyYjMyZDEyNTljN2M0YjcyZGM4Yzg3ZDhhOGI3NzM3MWRiNjBlODczOWQ2MDY5ODZiNjA5OTY3MGQ0NDY4ZTM2NjdmMTJiNDAzOWY5ZTM1YzE0OGEyODAwNmViYTg5MGQ1ZmM0MzgyM2QyMmY0MTlkZTg4ZTk0NGMxMDEzOWQwOGZhZGQ2ZjdmZGYwYTUyOTgz; BDSVRTM=0; H_PS_645EC=baa5N4fX02vKkLmMMqCLzvj7JbZ0EF2dnZYQekyPx6%2FP0bEHqRGo2jP%2Bujg; BA_HECTOR=852ha1akaha1ah81eb1h59knl0r; channel=baidusearch; baikeVisitId=8e56d9d5-1113-4159-8b14-c3db33e984b3'
        }

    def get_data(self):
        # 获取数据
        response = requests.get(self.url, self.headers)
        # 数据返回
        return response.content.decode('utf-8')

    def save_data(self, data):
        # 保存数据
        with open('tieba_python.html', 'w', encoding='utf-8') as f:
            f.write(data)
        print('保存成功')

    def start(self):
        data = self.get_data()
        self.save_data(data)


if __name__ == '__main__':
    t = Tieba()
    t.start()
